import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(private readonly jwtService: JwtService) {}

  checkAuth(headers: any) {
    // 获取 Header 里的 Authorization
    const token = headers.authorization;
    try {
      const userInfo = this.jwtService.verify(token);
      // 用户校验失败或者过期
      if (!userInfo || !userInfo['userid'] || userInfo['userid'] === '') {
        return false;
      }
      return userInfo;
    } catch (err) {
      // Token 验证失败
      return false;
    }
  }
}
